package com.yuanduo_app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.xujl.fastlib.utils.LogS;
import com.xujl.fastlib.utils.ToastUtils;

import com.yuanduo_app.consts.FileConst;
import com.yuanduo_app.service.UpdateService;

import java.io.File;

import androidx.core.content.FileProvider;

public class InstallUtils {
    private static final String TAG = UpdateService.TAG;

    public static Intent getInstallIntent (Context context) {
        LogS.d(TAG, "startInstall");
        //点击安装代码块
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(FileConst.UPDATE_PATH + FileConst.UPDATE_APK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(context, FileConst.fileProvider, file);
//            context.grantUriPermission("com.yuanduo_app", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        return intent;
    }

    public static boolean haveInstallPermission (Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return true;
        }
        return context.getPackageManager().canRequestPackageInstalls();
    }

    public static void jumpInstallPermission (Activity activity, int installPermissCode) {
        Uri packageURI = Uri.parse("package:" + activity.getApplication().getPackageName());
        Intent intent = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
        }
        activity.startActivityForResult(intent, installPermissCode);
    }

    public static void installApk (Context con) {
        String filePath = FileConst.UPDATE_PATH + FileConst.UPDATE_APK;
        try {
            if (TextUtils.isEmpty(filePath)) {
                LogS.e(TAG, "apk文件路径为空");
                return;
            }
            File file = new File(filePath);
            if (!file.exists()) {
                ToastUtils.toast("下载文件不存在");
                LogS.e(TAG, "下载文件不存在!");
                return;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//增加读写权限
            }
            intent.setDataAndType(getPathUri(con, filePath), "application/vnd.android.package-archive");
            con.startActivity(intent);
        } catch (
                Exception e) {
            e.printStackTrace();
            LogS.e(TAG, "安装异常：" + e.getMessage());
            ToastUtils.toast("安装失败，请重新下载");
        } catch (
                Error error) {
            error.printStackTrace();
            LogS.e(TAG, "安装异常：" + error.getMessage());
            ToastUtils.toast("安装失败，请重新下载");
        }
    }

    private static Uri getPathUri (Context context, String filePath) {
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, FileConst.fileProvider, new File(filePath));
        } else {
            uri = Uri.fromFile(new File(filePath));
        }
        return uri;
    }
}
